#!/usr/bin/env bash
set -e
# disable swap

swapoff -a
sed -i /swap/d /etc/fstab

# disable firewalld & selinux
systemctl disable --now firewalld
setenforce 0 || :

sed -i s/SELINUX=enforcing/SELINUX=permissive/g /etc/sysconfig/selinux || :

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

modprobe br_netfilter

cat <<EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.ipv6.ip_forward = 1
EOF
sysctl --system

# install containerd


dnf install -y yum-utils
yum-config-manager \
    --add-repo \
    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
dnf install -y containerd.io
sed -i '/disabled_plugins/d' /etc/containerd/config.toml
systemctl enable --now containerd

ctr -n k8s.io images pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2
ctr -n k8s.io images tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 k8s.gcr.io/pause:3.2

# install kubernetes

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

dnf install -y --nogpgcheck kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable --now kubelet

curl https://docs.projectcalico.org/manifests/calico.yaml -O
sed -i 's/docker.io/zhmpidnm.mirror.aliyuncs.com/g' calico.yaml
